package com.lcxw.hxzbapi.controller.incomecontroller;

import com.lcxw.hxzbapi.entity.pojo.Income;
import com.lcxw.hxzbapi.entity.pojo.IncomeTotal;
import com.lcxw.hxzbapi.entity.pojo.Proportion;
import com.lcxw.hxzbapi.entity.pojo.YwpdMember;
import com.lcxw.hxzbapi.entity.requestBean.RequestParamBasic;
import com.lcxw.hxzbapi.entity.response.GetMyIncomeResp;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.IncomeService;
import com.lcxw.hxzbapi.service.IncomeTotalService;
import com.lcxw.hxzbapi.service.YwpdMemberService;
import com.lcxw.hxzbcommon.common.StringUtil;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class IncomeRealization {

    @Resource
    IncomeService incomeService;

    @Resource
    IncomeTotalService incomeTotalService;

    @Resource
    YwpdMemberService ywpdMemberService;

    @Resource
    Proportion proportion;

    public void addIncome(long userId, long fromUserId, double rmb, String type, String addTime) {
        //在收入记录中增加一条记录
        Income income = new Income();
        income.setUserId(userId);
        income.setFromUserId(fromUserId);
        income.setRmb(rmb);
        income.setType(type);
        income.setAddTime(addTime);
        incomeService.insert(income);
        double total = 0;
        double incomeToUnion = 0;

        double incomeEnable = 0;
        YwpdMember ywpdMember = ywpdMemberService.selectYwpdMemberByUserId(userId);

        //如果未签约，则该收入计入可提现收入
        if (ywpdMember.getSign() == 0) {
            incomeEnable = StringUtil.formatDouble(
                rmb * proportion.getOrdinaryProp() * (1 - proportion.getOrdinaryTax()));
            total = StringUtil.formatDouble(rmb * proportion.getOrdinaryProp());//未签约用户，只包含70%的分成
        } else {
            //签约用户，总收入为分成后的收入，税前收入，税收将有公会开发票给平台报账
            total = StringUtil.formatDouble(rmb * proportion.getUnionProp());
            incomeToUnion = total;
        }

        IncomeTotal incomeTotal = incomeTotalService.selectByUserId(userId);
        if (null == incomeTotal) {

            IncomeTotal newIncomeTotal = new IncomeTotal();
            newIncomeTotal.setUserId(userId);
            newIncomeTotal
                .setHistoryIncome(StringUtil.formatDouble(rmb * proportion.getOrdinaryProp()));
            newIncomeTotal.setIncomeTotal(total);
            newIncomeTotal.setIncomeEnable(incomeEnable);
            newIncomeTotal.setIncomeFish(0.00);
            newIncomeTotal.setIncomeToUnion(incomeToUnion);
            incomeTotalService.insert(newIncomeTotal);

            return;
        }
        incomeTotal
            .setHistoryIncome(StringUtil
                .formatDouble(incomeTotal.getHistoryIncome() + rmb * proportion.getOrdinaryProp()));
        incomeTotal.setIncomeTotal(
            StringUtil.formatDouble(incomeTotal.getIncomeTotal() + total));
        incomeTotal
            .setIncomeEnable(StringUtil.formatDouble(incomeTotal.getIncomeEnable() + incomeEnable));
        incomeTotal.setIncomeToUnion(
            StringUtil.formatDouble(incomeTotal.getIncomeToUnion() + incomeToUnion));
        incomeTotalService.update(incomeTotal);

    }

    public Response getMyIncome(RequestParamBasic requestBody) {
        long userId = Long.parseLong(requestBody.getUserId());
        IncomeTotal incomeTotal = incomeTotalService.selectByUserId(userId);
        GetMyIncomeResp getMyIncomeResp = new GetMyIncomeResp();

        if (incomeTotal == null) {
            return Response.ok(new GetMyIncomeResp());
        }

        double incomeEnable = incomeTotal.getIncomeEnable();

        double historyIncome = incomeTotal.getHistoryIncome();

        getMyIncomeResp.setIncomeEnable(incomeEnable);
        getMyIncomeResp.setHistoryIncome(historyIncome);

        String today = StringUtil.getDateTime();
        today = today.split(" ")[0];
        String from = today + " " + "00:00:00";
        String to = today + " " + "23:59:59";
        double todayIncome = incomeService.getTodayIncome(userId, from, to);
        todayIncome = StringUtil.formatDouble(todayIncome * proportion.getOrdinaryProp());

        getMyIncomeResp.setIncomeToday(StringUtil.formatDouble(todayIncome));

        return Response.ok(getMyIncomeResp);

    }
}
